package com.collins.io.practice;

import org.apache.log4j.Logger;

import java.io.File;

public class IterDir {
    public static Logger logger = Logger.getLogger(IterDir.class);

    public static void main(String[] args) {
        iter("/Users/collinsliu/learning/vue-projects/nios/src/main/java");
    }

    public static void iter(String path) {
        int lvl = 0;
        File file = new File(path);
        if (file.exists()) {
            iterFile(file, lvl);
        }
    }

    public static void iterFile(File file, int lvl) {
        if (file.isDirectory()) {
            log(lvl, file.getName(), "d");
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    lvl++;
                    if (f.isDirectory()) {
                        iterFile(f, lvl);
                    } else {
                        log(lvl, f.getName(), "f");
                    }
                    lvl--;
                }
            }
        } else {
            // TODO if first level path is already a file
            log(lvl, file.getName(), "f");
        }
    }

    public static void log(int lvl, String name, String type) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lvl; i++) {
            sb.append("*");
        }
        logger.info(type + " " + sb.toString() + name);
    }
}
